home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / dakit / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-12  |  6.7 KB  |  256 lines

  1. /*--misc.c----------------------------------------------------------
  2.  * Miscellaneous routines.
  3.  */
  4.  
  5. #include "main.h"
  6. #include "anim.h"
  7.  
  8. #define local static
  9.  
  10. extern BOOL EscFlag;
  11. extern WORD curFrame;
  12. extern BITMAP hidbm, prevFrame;
  13.  
  14. /* --------------- BugExit, EmergencyExit ---------------
  15.  */
  16. BugExit(char *s)
  17. {
  18.     printf("Program Bug: %s.\n", s);
  19.     exit(1);
  20. }
  21.  
  22. EmergencyExit(char *s)
  23. {
  24.     printf("%s.\n", s);
  25.     exit(1);
  26. }
  27.  
  28. NotEnoughMemExit()
  29. {
  30.     EmergencyExit("Not enough memory to run this program");
  31. }
  32.  
  33. /* --------------- AbortKeyHit, CheckAbort ---------------
  34.  */
  35. #define    ESC        0x1b
  36.  
  37. local BOOL AbortKeyHit()
  38. {
  39.     register WORD i;
  40.  
  41.     while (char_avail()) {    /* Consume keys, looking for ESC. */
  42.         i = get_char();
  43.         if (i == ESC) {
  44.             EscFlag = TRUE;    /* Remember ESC was hit. */
  45.             return TRUE;
  46.         }
  47.     }
  48.     return FALSE;    /* Abort key not seen. */
  49. }
  50.  
  51. /* Once abort key has been hit, further checks see EscFlag. */
  52. BOOL CheckAbort()
  53. {
  54.     return (EscFlag || AbortKeyHit());
  55. }
  56.  
  57. /* ---------------  --------------- */
  58. #define bitmap_offs(bm, x, y)    (x + (bm)->width * (y))
  59. #define BytesNeeded(pix)        EVEN_UP(pix)        /* one byte-per-pixel */
  60.     /* Must be whole # words. */
  61.  
  62. /* --------------- BMLineStart ---------------
  63.  * Returns a far pointer to the start of line y
  64.  * of the given plane in the bitmap.
  65.  */
  66.  
  67. WORD far *BMLineStart(BITMAP *bm, WORD y, WORD plane)
  68. {
  69.     return (WORD_FAR_PTR(bm->seg[plane], bitmap_offs(bm, 0, y)));
  70. }
  71.  
  72. /* --------------- init_bitmap ---------------
  73.  */
  74. init_bitmap(bm, width, height)
  75. register BITMAP *bm;
  76. WORD width, height;
  77. {
  78.     bm->width = BytesNeeded(width);
  79.     bm->box.x = 0;
  80.     bm->box.y = 0;
  81.     bm->box.w = width;
  82.     bm->box.h = height;
  83.     bm->planes = curDepth;
  84. }
  85.  
  86. /* --------------- NewBitMap ---------------
  87.  * CAUTION: only call when have NOT allocated bm->seg[]'s.
  88.  */
  89. NewBitMap(BITMAP *bm, WORD width, WORD height)
  90. {
  91.     init_bitmap(bm, width, height);
  92.     bm->seg[0] = DAlloc(N_BYTES_IN_BITMAP);
  93.     if (!bm->seg[0])
  94.         NotEnoughMemExit();
  95. }
  96.  
  97. /* --------------- SetBitMap ---------------
  98.  */
  99. void SetBitMap(BITMAP *bm, WORD color)
  100. {
  101.     far_setmem(bm->seg[0], 0, N_BYTES_IN_BITMAP, color);
  102. }
  103.  
  104. /* --------------- FirstAnimFrame ---------------
  105.  * Switch to first frame for editing.
  106.  */
  107.  
  108. /* Read into lpBuffer, but don't transfer to hidbm.
  109.  * This gets all the pointers correctly set-up, so that hidbm can
  110.  * become the new first frame (by calling AnimSetDirtyBox).
  111.  */
  112. FirstAnimFrame00() {
  113.     curFrame = FIRST_FRAME_N;
  114.     ReadLpfFrame(curFrame);
  115.     }
  116.  
  117.  
  118. FirstAnimFrame0() {
  119.     /* --- Clear junk from prevFrame. */
  120.     far_setmem(BSEG(&prevFrame), 0, N_BYTES_IN_BITMAP, white_color);
  121.  
  122.     FirstAnimFrame00();
  123.     PlayDeltaFrame(&hidbm);
  124.     }
  125.  
  126. /* --------------- CopyHidbmToPrevFrame ---------------
  127.  */
  128. local void CopyHidbmToPrevFrame(void) {
  129.     far_movmem( BSEG(&hidbm), 0,
  130.                 BSEG(&prevFrame), 0,  N_BYTES_IN_BITMAP );
  131.     }
  132.  
  133. /* --------------- NextAnimFrame0 ---------------
  134.  * Switch to next frame for editing.
  135.  */
  136. NextAnimFrame0() {
  137.     curFrame++;
  138.  
  139.     if (curFrame > LAST_FRAME_N)
  140.         FirstAnimFrame0();
  141.     else {
  142.         /* (old) current frame is (next) prevFrame.
  143.          * Don't use PlayDeltaFrame, because didn't keep Delta in RAM. */
  144.         CopyHidbmToPrevFrame();
  145.         ReadLpfFrame(curFrame);
  146.         PlayDeltaFrame(&hidbm);
  147.             /* Convert old current frame to new current frame. */
  148.     }
  149. }
  150.  
  151. /* -------------------------------------------------------------------------
  152.  *        Disk i/o with critical error detection.
  153.  * -------------------------------------------------------------------------
  154.  */
  155. /* returns -1 if it can't read the full number of bytes */
  156. int readfull(int file, char *buffer, unsigned int count) {
  157.     return (readdos(file,dataseg(),buffer,count) == count) ? 0: -1;
  158.     }
  159.  
  160. /* returns -1 if it can't read the full number of bytes */
  161. int readdosfull(UWORD file, UWORD seg, UWORD buffer, UWORD count) {
  162.     return (readdos(file,seg,buffer,count) == count) ? 0: -1;
  163.     }
  164.  
  165. /* returns -1 if it can't write the full number of bytes */
  166. int writefull(int file, char *buffer, unsigned int count) {
  167.     return (writedos(file,dataseg(),buffer,count) == count) ? 0: -1;
  168.     }
  169.  
  170. /* returns -1 if it can't write the full number of bytes */
  171. int writedosfull(UWORD file, UWORD seg, UWORD buffer, UWORD count) {
  172.     return (writedos(file,seg,buffer,count) == count) ? 0: -1;
  173.     }
  174.  
  175. long lseekdos(int file, long offset, int origin) {
  176.     long result = lseek(file,offset,origin);
  177.     return result;
  178.     }
  179.  
  180. /*-----------------------------------------------------------------
  181.  * --------------- do_num_to_string -------------------------------
  182.  * Convert a number to a string.  Parameters:
  183.  *    UWORD num = # to convert to string.
  184.  *    char *string = Ptr to string where you want the #.
  185.  *    UWORD length = # of chars you want in the resulting string.  Set to -1
  186.  *        if you don't require any specific string length.  If the string
  187.  *        is too short, it will be padded with blanks on the left.  If it
  188.  *        is too long, it will be chopped off on the right.
  189.  *    BOOL negative = Set to TRUE if you want numbers > 32767 to be printed
  190.  *        as negative (with a leading "-"), or FALSE otherwise.
  191.  */
  192.  
  193. /* TRUE before calling do_num_to_string, if want padding with 0's
  194.  * instead of blanks.  Must set back to FALSE when done, so
  195.  * other clients will get ' 's.
  196.  */
  197. local BOOL padWithZeroes = FALSE;
  198.  
  199. void do_num_to_string(UWORD num, char *string, UWORD length, BOOL negative)
  200. {
  201.     UWORD temp;
  202.     register UWORD index, i;
  203.     BOOL start;
  204.  
  205.     index = 0;
  206.  
  207.     /* --- does caller want negative # printed with leading '-'? */
  208.     if (negative && num >= 0x8000) {
  209.         if (index >= length)   goto terminate;
  210.         num = -num;
  211.         string[index++] = '-';
  212.     }
  213.  
  214.     /* --- convert the # to a string */
  215.     if (num == 0) {
  216.         if (index >= length)   goto terminate;
  217.         string[index++] = '0';
  218.     } else {
  219.         start = FALSE;
  220.         for (i = 10000; i > 0; i /= 10) {
  221.             temp = (num / i);
  222.             if (temp || start) {
  223.                 /* --- if string has reached max length, stop */
  224.                 if (index >= length)   goto terminate;
  225.                 string[index++] = temp + '0';
  226.                 start = TRUE;
  227.             }
  228.             num %= i;
  229.         }
  230.     }
  231.  
  232.     /* --- terminate the string */
  233. terminate:
  234.     string[index] = 0;
  235.  
  236.     /* --- if caller wants string to be at least a certain length, do it */
  237.     i = strlen(string);
  238.     if ((WORD)i < (WORD)length) {
  239.         /* --- string is too short, so add spaces to left */
  240.         while ((WORD)i < (WORD)length) {
  241.             movmem(string, string + 1, i + 1);
  242.             string[0] = (padWithZeroes ?  '0' :  ' ');
  243.             ++i;
  244.         }
  245.     }
  246. }
  247.  
  248. /* --------------- num_to_string_zeroes ---------------
  249.  * Specified length, no negative, padding with zeroes.
  250.  */
  251. void num_to_string_zeroes(UWORD num, char *string, UWORD length) {
  252.     padWithZeroes = TRUE;
  253.     do_num_to_string(num, string, length, FALSE);
  254.     padWithZeroes = FALSE;
  255. }
  256.